﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace Reconstruct_Desktop.Convert
{
  public  class GuidToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string inputString = value as string;
            if (string.IsNullOrEmpty(inputString))
                return System.Windows.Media.Brushes.LightGray; // 默认颜色（无值时）

            // 计算输入字符串的哈希码
            int hashCode = inputString.GetHashCode();

            // 计算颜色值（基于字符串的哈希码）
            int hue = (hashCode % 360) + 0; // 色相范围 0-360
            double saturation = 0.6; // 饱和度（0-1）
            double brightness = 0.8; // 亮度（0-1）

            System.Windows.Media.Color color = HSVToColor(hue, saturation, brightness);
            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        // HSV 转 RGB 的辅助方法（与之前相同）
        private static System.Windows.Media.Color HSVToColor(int hue, double saturation, double brightness)
        {
            int h = hue;
            double s = saturation;
            double v = brightness;

            int hi = h / 60 % 6;
            double f = (h / 60.0) - hi;
            double p = v * (1 - s);
            double q = v * (1 - f * s);
            double t = v * (1 - (1 - f) * s);

            byte r, g, b;
            switch (hi)
            {
                case 0:
                    r = (byte)(v * 255);
                    g = (byte)(t * 255);
                    b = (byte)(p * 255);
                    break;
                case 1:
                    r = (byte)(q * 255);
                    g = (byte)(v * 255);
                    b = (byte)(p * 255);
                    break;
                case 2:
                    r = (byte)(p * 255);
                    g = (byte)(v * 255);
                    b = (byte)(t * 255);
                    break;
                case 3:
                    r = (byte)(p * 255);
                    g = (byte)(q * 255);
                    b = (byte)(v * 255);
                    break;
                case 4:
                    r = (byte)(t * 255);
                    g = (byte)(p * 255);
                    b = (byte)(v * 255);
                    break;
                default:
                    r = (byte)(v * 255);
                    g = (byte)(p * 255);
                    b = (byte)(q * 255);
                    break;
            }

            return System.Windows.Media.Color.FromArgb(125, r, g, b);
        }

    }
}
